from django.contrib import admin
from apps.news.models import Record
from models import Comment

class CommentAdmin(admin.ModelAdmin):
    list_display = ('format_user', 'date', 'format_message', 'format_link')

    def format_link(self, comment):
        record = Record.objects.get(pk = comment.uid)
        return "<a href='%s' target='_blank'>%s</a>" % (record.get_page_url(), record.title)
    format_link.allow_tags = True

    def format_message(self, comment):
      try:
        comment.message = comment.message.replace("<", "&lt;")
        comment.message = comment.message.replace(">", "&gt;")

        for key in comment.SMILES:
            str = ":%s:" % key
            replace = "<img src='%s' />" % comment.SMILES.get(key)
            comment.message = comment.message.replace(str, replace)

        return comment.message
      except Exception, e:
	return "Error message!"
    format_message.allow_tags = True

    def format_user(self, comment):
      try:
        ret = "%s (%s)<br/><img src='%s' width='50' height='50' />" % (comment.user.get_profile().get_user_name(),Comment.objects.filter(user=comment.user).count(),comment.user.get_profile().get_avatar())
        return ret
      except Exception, e:
        return "Err.: %s" % comment.user
    format_user.allow_tags = True

admin.site.register(Comment, CommentAdmin)
